<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="70%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('project.project.custom_id')" type="remoteSelect"
                              v-model="baTable.form.items!.custom_id" prop="custom_id"
                              :input-attr="{ pk: 'ba_custom.id', field: 'username', remoteUrl: '/admin/Custom/index' }"
                              :placeholder="t('Please select field', { field: t('project.project.custom_id') })"/>
                    <FormItem :label="t('project.project.project_cate_id')" type="remoteSelect"
                              v-model="baTable.form.items!.project_cate_id" prop="project_cate_id"
                              :input-attr="{ pk: 'ba_project_cate.id', field: 'project_cate', remoteUrl: '/admin/project.Cate/index' }"
                              :placeholder="t('Please select field', { field: t('project.project.project_cate_id') })"/>
                    <FormItem :label="t('project.project.project_name')" type="string"
                              v-model="baTable.form.items!.project_name" prop="project_name"
                              :placeholder="t('Please input field', { field: t('project.project.project_name') })"/>
                    <FormItem :label="t('project.project.total_money')" type="number" prop="total_money"
                              :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.total_money"
                    />


                    <FormItem :label="t('project.project.status')" type="radio" v-model="baTable.form.items!.status"
                              prop="status" :input-attr="{ content: {0:'开发中',1:'已交付'} }"
                              :placeholder="t('Please select field', { field: t('project.project.status') })"/>
                    <FormItem :label="t('project.project.annex')" type="files" v-model="baTable.form.items!.annex"
                              prop="annex"
                    />
<!--                    <FormItem :label="t('project.project.ip')" type="string" v-model="baTable.form.items!.ip" prop="ip"-->
<!--                              :placeholder="t('Please input field', { field: t('project.project.ip') })"/>-->
<!--                    <FormItem :label="t('project.project.expire_time')" type="date"-->
<!--                              v-model="baTable.form.items!.expire_time" prop="expire_time"-->
<!--                              :placeholder="t('Please select field', { field: t('project.project.expire_time') })"/>-->
                    <FormItem :label="t('project.project.notes')" type="string" v-model="baTable.form.items!.notes"
                              prop="notes"
                              :placeholder="t('Please input field', { field: t('project.project.notes') })"/>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="submits(formRef)"
                           type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref, watch} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'

const dynamicInputs = ref([] as {
    type: string;
    amount: number;
    time: string;
}[]);
// 减少输入框的方法
const removeInput = (index: number) => {
    dynamicInputs.value.splice(index, 1);
};

const addInput = () => {
    dynamicInputs.value.push({
        type: '',
        amount: null,
        time: '',
    });
};

const submits = (formRef) => {
    baTable.form.items.income = dynamicInputs.value
    baTable.onSubmit(formRef)
}


const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
// 打开表单时刷新用户数据
watch(
    () => baTable.form.items,
    (newValue) => {
        if (newValue) {
            if (newValue.income) {
                dynamicInputs.value = newValue.income
            } else {
                dynamicInputs.value = [];
            }

        }
    }
)

const {t} = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    custom_id: [buildValidatorData({name: 'required', title: t('project.project.custom_id')})],
    project_cate_id: [buildValidatorData({name: 'required', title: t('project.project.project_cate_id')})],
    project_name: [buildValidatorData({name: 'required', title: t('project.project.project_name')})],
    total_money: [buildValidatorData({name: 'required', title: t('project.project.total_money')})],
})


</script>

<style scoped lang="scss">
.input-container {
    display: flex;
    margin-left: 156px;
    margin-bottom: 14px;
}


</style>
